<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="../Styles/style.css" rel="stylesheet" type="text/css"/>


</head>

<body>
<div class="article-body">
    <div class="article-intro">

        <h1>Java 多态</h1>
        <hr>

        <p>多态是同一个行为具有多个不同表现形式或形态的能力。</p>
        <p>多态就是同一个接口，使用不同的实例而执行不同操作，如图所示：</p>
        <p></p>
        <p><img src="//www.runoob.com/wp-content/uploads/2013/12/dt-java.png"></p>
        <p>多态性是对象多种表现形式的体现。</p>

        <blockquote>
            <p>现实中，比如我们按下 F1 键这个动作：
            </p>
            <ul><li>
                如果当前在 Flash 界面下弹出的就是 AS 3 的帮助文档；</li><li>如果当前在 Word 下弹出的就是 Word 帮助；</li><li>在 Windows 下弹出的就是 Windows 帮助和支持。</li></ul><p>同一个事件发生在不同的对象上会产生不同的结果。</p>
        </blockquote>
        <h3>
            多态的优点
        </h3><ul><li>
        1. 消除类型之间的耦合关系</li><li>
        2. 可替换性</li><li>
        3. 可扩充性</li><li>
        4. 接口性</li><li>
        5. 灵活性</li><li>
        6. 简化性</li></ul>

        <h3>多态存在的三个必要条件</h3>
        <ul><li>继承</li><li>重写</li><li>父类引用指向子类对象</li></ul><p>
        比如：</p><pre class="prettyprint prettyprinted" style=""><span class="typ">Parent</span><span class="pln"> p </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">Child</span><span class="pun">();</span></pre>
        <p>当使用多态方式调用方法时，首先检查父类中是否有该方法，如果没有，则编译错误；如果有，再去调用子类的同名方法。</p>
        <p>多态的好处：可以使程序有良好的扩展，并可以对所有类的对象进行通用处理。</p>
        <p>以下是一个多态实例的演示，详细说明请看注释：</p>
        <div class="example">
            <h2 class="example">Test.java 文件代码：</h2>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Test</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-identifier">show</span><span class="hl-brackets">(</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Cat</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">;  </span><span class="hl-comment">//</span><span class="hl-comment"> 以 Cat 对象调用 show 方法</span><span class="hl-comment"></span><span class="hl-code">
      </span><span class="hl-identifier">show</span><span class="hl-brackets">(</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">;  </span><span class="hl-comment">//</span><span class="hl-comment"> 以 Dog 对象调用 show 方法</span><span class="hl-comment"></span><span class="hl-code">

      </span><span class="hl-identifier">Animal</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Cat</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;  </span><span class="hl-comment">//</span><span class="hl-comment"> 向上转型  </span><span class="hl-comment"></span><span class="hl-code">
      </span><span class="hl-identifier">a</span><span class="hl-code">.</span><span class="hl-identifier">eat</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;               </span><span class="hl-comment">//</span><span class="hl-comment"> 调用的是 Cat 的 eat</span><span class="hl-comment"></span><span class="hl-code">
      </span><span class="hl-identifier">Cat</span><span class="hl-code"> </span><span class="hl-identifier">c</span><span class="hl-code"> = </span><span class="hl-brackets">(</span><span class="hl-identifier">Cat</span><span class="hl-brackets">)</span><span class="hl-identifier">a</span><span class="hl-code">;        </span><span class="hl-comment">//</span><span class="hl-comment"> 向下转型  </span><span class="hl-comment"></span><span class="hl-code">
      </span><span class="hl-identifier">c</span><span class="hl-code">.</span><span class="hl-identifier">work</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;        </span><span class="hl-comment">//</span><span class="hl-comment"> 调用的是 Cat 的 work</span><span class="hl-comment"></span><span class="hl-code">
  </span><span class="hl-brackets">}</span><span class="hl-code">

    </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">show</span><span class="hl-brackets">(</span><span class="hl-identifier">Animal</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-brackets">)</span><span class="hl-code">  </span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-identifier">a</span><span class="hl-code">.</span><span class="hl-identifier">eat</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-comment">//</span><span class="hl-comment"> 类型判断</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">a</span><span class="hl-code"> </span><span class="hl-reserved">instanceof</span><span class="hl-code"> </span><span class="hl-identifier">Cat</span><span class="hl-brackets">)</span><span class="hl-code">  </span><span class="hl-brackets">{</span><span class="hl-code">  </span><span class="hl-comment">//</span><span class="hl-comment"> 猫做的事情 </span><span class="hl-comment"></span><span class="hl-code">
            </span><span class="hl-identifier">Cat</span><span class="hl-code"> </span><span class="hl-identifier">c</span><span class="hl-code"> = </span><span class="hl-brackets">(</span><span class="hl-identifier">Cat</span><span class="hl-brackets">)</span><span class="hl-identifier">a</span><span class="hl-code">;
            </span><span class="hl-identifier">c</span><span class="hl-code">.</span><span class="hl-identifier">work</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">else</span><span class="hl-code"> </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">a</span><span class="hl-code"> </span><span class="hl-reserved">instanceof</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-comment">//</span><span class="hl-comment"> 狗做的事情 </span><span class="hl-comment"></span><span class="hl-code">
            </span><span class="hl-identifier">Dog</span><span class="hl-code"> </span><span class="hl-identifier">c</span><span class="hl-code"> = </span><span class="hl-brackets">(</span><span class="hl-identifier">Dog</span><span class="hl-brackets">)</span><span class="hl-identifier">a</span><span class="hl-code">;
            </span><span class="hl-identifier">c</span><span class="hl-code">.</span><span class="hl-identifier">work</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-brackets">}</span><span class="hl-code">
    </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">

</span><span class="hl-reserved">abstract</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Animal</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">abstract</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">eat</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">

</span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Cat</span><span class="hl-code"> </span><span class="hl-reserved">extends</span><span class="hl-code"> </span><span class="hl-identifier">Animal</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">eat</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">吃鱼</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
    </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">work</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">抓老鼠</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">

</span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Dog</span><span class="hl-code"> </span><span class="hl-reserved">extends</span><span class="hl-code"> </span><span class="hl-identifier">Animal</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">eat</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">吃骨头</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
    </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">work</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">看家</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div>
        </div>
        <p>执行以上程序，输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">吃鱼</span><span class="pln">
</span><span class="pun">抓老鼠</span><span class="pln">
</span><span class="pun">吃骨头</span><span class="pln">
</span><span class="pun">看家</span><span class="pln">
</span><span class="pun">吃鱼</span><span class="pln">
</span><span class="pun">抓老鼠</span></pre>

        <hr>
        <h2>虚方法</h2>
        <p>我们将介绍在Java中，当设计类时，被重写的方法的行为怎样影响多态性。</p><p>
        我们已经讨论了方法的重写，也就是子类能够重写父类的方法。</p><p>
        当子类对象调用重写的方法时，调用的是子类的方法，而不是父类中被重写的方法。</p><p>
        要想调用父类中被重写的方法，则必须使用关键字super。</p>
        <div class="example">
            <h2 class="example">Employee.java 文件代码：</h2>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment">/*</span><span class="hl-comment"> 文件名 : Employee.java </span><span class="hl-comment">*/</span><span class="hl-code">
</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Employee</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
   </span><span class="hl-reserved">private</span><span class="hl-code"> </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">name</span><span class="hl-code">;
   </span><span class="hl-reserved">private</span><span class="hl-code"> </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">address</span><span class="hl-code">;
   </span><span class="hl-reserved">private</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">number</span><span class="hl-code">;
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">Employee</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">name</span><span class="hl-code">, </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">address</span><span class="hl-code">, </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">number</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">Employee 构造函数</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-reserved">this</span><span class="hl-code">.</span><span class="hl-identifier">name</span><span class="hl-code"> = </span><span class="hl-identifier">name</span><span class="hl-code">;
      </span><span class="hl-reserved">this</span><span class="hl-code">.</span><span class="hl-identifier">address</span><span class="hl-code"> = </span><span class="hl-identifier">address</span><span class="hl-code">;
      </span><span class="hl-reserved">this</span><span class="hl-code">.</span><span class="hl-identifier">number</span><span class="hl-code"> = </span><span class="hl-identifier">number</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">mailCheck</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">邮寄支票给： </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-reserved">this</span><span class="hl-code">.</span><span class="hl-identifier">name</span><span class="hl-code">
       + </span><span class="hl-quotes">"</span><span class="hl-string"> </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-reserved">this</span><span class="hl-code">.</span><span class="hl-identifier">address</span><span class="hl-brackets">)</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">toString</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">name</span><span class="hl-code"> + </span><span class="hl-quotes">"</span><span class="hl-string"> </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">address</span><span class="hl-code"> + </span><span class="hl-quotes">"</span><span class="hl-string"> </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">number</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">getName</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">name</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">getAddress</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">address</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">setAddress</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">newAddress</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-identifier">address</span><span class="hl-code"> = </span><span class="hl-identifier">newAddress</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">getNumber</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
     </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">number</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div>
        </div>
        <p>假设下面的类继承Employee类：</p>
        <div class="example">
            <h2 class="example">Salary.java 文件代码：</h2>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment">/*</span><span class="hl-comment"> 文件名 : Salary.java </span><span class="hl-comment">*/</span><span class="hl-code">
</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Salary</span><span class="hl-code"> </span><span class="hl-reserved">extends</span><span class="hl-code"> </span><span class="hl-identifier">Employee</span><span class="hl-code">
</span><span class="hl-brackets">{</span><span class="hl-code">
   </span><span class="hl-reserved">private</span><span class="hl-code"> </span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">salary</span><span class="hl-code">; </span><span class="hl-comment">//</span><span class="hl-comment"> 全年工资</span><span class="hl-comment"></span><span class="hl-code">
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">Salary</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">name</span><span class="hl-code">, </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">address</span><span class="hl-code">, </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">number</span><span class="hl-code">, </span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">salary</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
       </span><span class="hl-reserved">super</span><span class="hl-brackets">(</span><span class="hl-identifier">name</span><span class="hl-code">, </span><span class="hl-identifier">address</span><span class="hl-code">, </span><span class="hl-identifier">number</span><span class="hl-brackets">)</span><span class="hl-code">;
       </span><span class="hl-identifier">setSalary</span><span class="hl-brackets">(</span><span class="hl-identifier">salary</span><span class="hl-brackets">)</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">mailCheck</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
       </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">Salary 类的 mailCheck 方法 </span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
       </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">邮寄支票给：</span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">getName</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">
       + </span><span class="hl-quotes">"</span><span class="hl-string"> ，工资为：</span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">salary</span><span class="hl-brackets">)</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">getSalary</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
       </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">salary</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">setSalary</span><span class="hl-brackets">(</span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">newSalary</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
       </span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-identifier">newSalary</span><span class="hl-code"> &gt;= </span><span class="hl-number">0</span><span class="hl-number">.0</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
          </span><span class="hl-identifier">salary</span><span class="hl-code"> = </span><span class="hl-identifier">newSalary</span><span class="hl-code">;
       </span><span class="hl-brackets">}</span><span class="hl-code">
   </span><span class="hl-brackets">}</span><span class="hl-code">
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">computePay</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">计算工资，付给：</span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">getName</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">salary</span><span class="hl-code">/</span><span class="hl-number">52</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div>
        </div>
        <p>现在我们仔细阅读下面的代码，尝试给出它的输出结果：</p>
        <div class="example">
            <h2 class="example">VirtualDemo.java 文件代码：</h2>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment">/*</span><span class="hl-comment"> 文件名 : VirtualDemo.java </span><span class="hl-comment">*/</span><span class="hl-code">
</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">VirtualDemo</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-identifier">Salary</span><span class="hl-code"> </span><span class="hl-identifier">s</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Salary</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">员工 A</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string">北京</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-number">3</span><span class="hl-code">, </span><span class="hl-number">3600</span><span class="hl-number">.00</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">Employee</span><span class="hl-code"> </span><span class="hl-identifier">e</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Salary</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">员工 B</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string">上海</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-number">2</span><span class="hl-code">, </span><span class="hl-number">2400</span><span class="hl-number">.00</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">使用 Salary 的引用调用 mailCheck -- </span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">s</span><span class="hl-code">.</span><span class="hl-identifier">mailCheck</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-special">\n</span><span class="hl-string">使用 Employee 的引用调用 mailCheck--</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">e</span><span class="hl-code">.</span><span class="hl-identifier">mailCheck</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div>
        </div>
        <p>以上实例编译运行结果如下：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="typ">Employee</span><span class="pln"> </span><span class="pun">构造函数</span><span class="pln">
</span><span class="typ">Employee</span><span class="pln"> </span><span class="pun">构造函数</span><span class="pln">
</span><span class="pun">使用</span><span class="pln"> </span><span class="typ">Salary</span><span class="pln"> </span><span class="pun">的引用调用</span><span class="pln"> mailCheck </span><span class="pun">--</span><span class="pln">
</span><span class="typ">Salary</span><span class="pln"> </span><span class="pun">类的</span><span class="pln"> mailCheck </span><span class="pun">方法</span><span class="pln">
</span><span class="pun">邮寄支票给：员工</span><span class="pln"> A </span><span class="pun">，工资为：</span><span class="lit">3600.0</span><span class="pln">

</span><span class="pun">使用</span><span class="pln"> </span><span class="typ">Employee</span><span class="pln"> </span><span class="pun">的引用调用</span><span class="pln"> mailCheck</span><span class="pun">--</span><span class="pln">
</span><span class="typ">Salary</span><span class="pln"> </span><span class="pun">类的</span><span class="pln"> mailCheck </span><span class="pun">方法</span><span class="pln">
</span><span class="pun">邮寄支票给：员工</span><span class="pln"> B </span><span class="pun">，工资为：</span><span class="lit">2400.0</span></pre>
        <h3>例子解析</h3>
        <ul><li>
            <p>实例中，实例化了两个 Salary 对象：一个使用 Salary 引用 s，另一个使用 Employee 引用 e。</p></li><li>
            <p>
                当调用 s.mailCheck() 时，编译器在编译时会在 Salary 类中找到  mailCheck()，执行过程 JVM 就调用 Salary 类的 mailCheck()。</p></li><li>
            <p>
                因为 e 是 Employee 的引用，所以调用 e 的 mailCheck() 方法时，编译器会去 Employee 类查找 mailCheck() 方法 。</p>
        </li><li>
            <p>
                在编译的时候，编译器使用 Employee 类中的 mailCheck() 方法验证该语句，
                但是在运行的时候，Java虚拟机(JVM)调用的是 Salary 类中的 mailCheck() 方法。</p></li>
        </ul>
        <p>
            以上整个过程被称为虚拟方法调用，该方法被称为虚拟方法。</p><p>
        Java中所有的方法都能以这种方式表现，因此，重写的方法能在运行时调用，不管编译的时候源代码中引用变量是什么数据类型。</p>
        <hr>
        <h2>多态的实现方式</h2>
        <h3>方式一：重写：
        </h3>
        这个内容已经在上一章节详细讲过，就不再阐述，详细可访问：<a href="//www.runoob.com/java/java-override-overload.html" target="_blank">Java 重写(Override)与重载(Overload)</a>。<p></p>
        <h3>方式二：接口
        </h3>
        <ul><li><p>
            1. 生活中的接口最具代表性的就是插座，例如一个三接头的插头都能接在三孔插座中，因为这个是每个国家都有各自规定的接口规则，有可能到国外就不行，那是因为国外自己定义的接口类型。</p></li><li><p>
            2. java中的接口类似于生活中的接口，就是一些方法特征的集合，但没有方法的实现。具体可以看 <a href="/java/java-interfaces.html" target="_blank">java接口</a> 这一章节的内容。</p></li></ul>
        <h3>
            方式三：抽象类和抽象方法
        </h3><p>
        详情请看 <a href="/java/java-abstraction.html" target="_blank">Java抽象类</a> 章节。</p>


    </div>
</div>
</body>
</html>
